import { Node } from "cc";
import { Main } from "../Main";
import { ShakeCtrl } from "./shake/ShakeCtrl";
import { DirShakeCtrl } from "./shake/DirShakeCtrl";
import { IShakeCtrl } from "./shake/IShakeCtrl";
import { ShakeConst } from "./shake/ShakeConst";
import { GameSceneView } from "../game/GameSceneView";

/**
 * 震动
 */
export class ShakeUtils {
    /**
     * 震动id列表
     * @type {any[]}
     */
    private static gSceneShakeTimeList:Array<number>=[];
    private static gSceneShake:ShakeCtrl;
    private static gSceneDirShake:DirShakeCtrl;

    /**
     * 显示对象震动
     */
    public static shakeNormal(target:Node, shakeType:number = ShakeConst.TYPE_SHAKE, shakeDataIndex:number = 1, shakeData?:any, shakeAngle?:number, shakeUpdateFunc?:Function):IShakeCtrl
    {
        let _quakeData:any = shakeData ? shakeData : ShakeConst["SHAKE_DATA_" + shakeDataIndex];
        let _angle:number = shakeAngle ? shakeAngle : 0;
        let _shakeCtrl:IShakeCtrl = shakeType == ShakeConst.TYPE_SHAKE ? new ShakeCtrl() : new DirShakeCtrl();
        _shakeCtrl.setData(target, _quakeData, _angle, shakeUpdateFunc);
        _shakeCtrl.startShake();
        return _shakeCtrl;
    }

    /**
     * 场景震动
     */
    public static shakeScene(delay:number = 0, shakeType:number = ShakeConst.TYPE_SHAKE, shakeDataIndex:number = 1, shakeData?:any, shakeAngle?:number):void
    {
        let _quakeData:any = shakeData ? shakeData : ShakeConst["SHAKE_DATA_" + shakeDataIndex];
        let _angle:number = shakeAngle ? shakeAngle : 0;
        if (delay > 0)
        {
            let _timeoutId:number = setTimeout(this.doShake, delay, shakeType, _quakeData, _angle);
            this.gSceneShakeTimeList.push(_timeoutId);
        }
        else
        {
            this.doShake(shakeType, _quakeData, _angle);
        }
    }

    private static doShake(cusShakeType:number, cusQuakeData:any, cusQuakeAngle:number):void
    {
        if (this.gSceneShake)
        {
            this.gSceneShake.stopShake()
        }
        if (this.gSceneDirShake)
        {
            this.gSceneDirShake.stopShake();
        }
        if (cusShakeType == ShakeConst.TYPE_SHAKE)
        {
            if (this.gSceneShake == null)
            {
                this.gSceneShake = new ShakeCtrl();
            }
            this.gSceneShake.setData(GameSceneView.instance.battleLayer, cusQuakeData);
            this.gSceneShake.startShake();
        }
        else if (cusShakeType == ShakeConst.TYPE_DIR_SHAKE)
        {
            if (this.gSceneDirShake == null)
            {
                this.gSceneDirShake = new DirShakeCtrl();
            }
            this.gSceneDirShake.setData(GameSceneView.instance.battleLayer, cusQuakeData, cusQuakeAngle/*, redrawScene*/);
            this.gSceneDirShake.startShake();
        }
    }

    public static cleanShake():void
    {
        if (this.gSceneShake)
        {
            this.gSceneShake.stopShake()
        }
        if (this.gSceneDirShake)
        {
            this.gSceneDirShake.stopShake();
        }
        let _timeId:number;
        while(this.gSceneShakeTimeList.length > 0)
        {
            _timeId = this.gSceneShakeTimeList.pop();
            clearTimeout(_timeId);
        }
    }
}